﻿using UnityEngine;
using System.Collections;
using System;
using System.Text;

namespace Game.Global.Helper
{
    /// <summary>
    /// 对于类的辅助操作
    /// </summary>
	public static class ClassHelper
    {
        /// <summary>
        /// 拷贝一个类的公开的值并返回一个对应的类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="src"></param>
        /// <returns></returns>
        public static T Copy<T>(T src) where T : new()
        {
            System.Type t = typeof(T);
            if (src == null)
            {
                throw new System.NullReferenceException("The class you want to copy is empty: " + t.Name);
            }

            T result = new T();

            var fields = t.GetFields();

            for (int i = 0; i < fields.Length; i++)
            {
                var currField = fields[i];

                object srcVal = currField.GetValue(src);
                currField.SetValue(result, srcVal);
            }

            return result;
        }

    }

}